using zhwy.saas.api.Entity.Sys;
using zhwy.saas.api.Extensions.Log;
using zhwy.saas.api.Extensions.Sql;
using zhwy.saas.api.Models;
using zhwy.saas.api.Models.Sys.Tenants;
using zhwy.saas.api.Repository;
using zhwy.saas.api.Services.Sinterface;
using zhwy.saas.api.Services.Sinterface.Sys;

namespace zhwy.saas.api.Services.Sys
{
    /// <summary>
    /// 系统租户服务实现
    /// </summary>
    public class SysTenantService : BaseService<TenantsEntity>, ISysTenantService
    {
        private readonly ICurrentUserService _currentUserService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">租户仓储接口</param>
        /// <param name="logger">日志服务</param>
        public SysTenantService(IBaseRepository<TenantsEntity> repository, ILogExt logger,
           ICurrentUserService currentUserService)
            : base(repository, logger)
        {
            _currentUserService = currentUserService;
        }



        /// <summary>
        /// 检查租户统一社会信用代码是否存在
        /// </summary>
        /// <param name="usci">统一社会信用代码</param>
        /// <returns>是否存在</returns>
        public async Task<bool> CheckusciExists(string usci)
        {
            var tenant = await _repository.DbContext.Queryable<TenantsEntity>()
                .Where(x => x.Usci == usci)
                .FirstAsync();
            return tenant != null ? true : false;
        }

        /// <summary>
        /// 分页获取实体列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PagedList<TenantsEntity>> GetPageList(GetPageListReq request)
        {
            // 初始化为始终为真的表达式
            var predicate = PredicateBuilder.New<TenantsEntity>(t => t.Id > 0);
            if (!string.IsNullOrEmpty(request.Usci))
            {
                predicate = predicate.And(p => p.Usci.Contains(request.Usci.Trim()));
            }
            if (!string.IsNullOrEmpty(request.Name))
            {
                predicate = predicate.And(p => p.Name.Contains(request.Name.Trim()));
            }
            if (request.Status >= 0)
            {
                predicate = predicate.And(p => p.Status == request.Status);
            }

            var list = await _repository.GetPagedListAsync(predicate, request.PageIndex, request.PageSize);
            var res = list.Items.Select(i => new TenantsEntity()
            {
                Businesslicense_img = i.Businesslicense_img,
                Createtime = i.Createtime,
                Id = i.Id,
                Legalperson = i.Legalperson,
                Name = i.Name,
                Status = i.Status,
                Telephone = i.Telephone,
                Tenant_code = i.Tenant_code,
                Usci = i.Usci,
                Updatetime = i.Updatetime,
            }).ToList();
            return new PagedList<TenantsEntity>()
            {
                Items = res,
                PageIndex = request.PageIndex,
                PageSize = request.PageSize,
                Total = list.Total,
            };
        }

        /// <summary>
        /// 修改租户信息
        /// </summary>
        /// <param name="req"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public async Task<bool> UpdateTenantInfo(UpdateTenantInfoReq req, long userid)
        {
            var tenant = await _repository.GetByIdAsync(req.Id);
            if (tenant == null)
            {
                throw new ArgumentException("租户不存在");
            }
            tenant.Updateuserid = userid;
            tenant.Updatetime = DateTime.Now;
            if (req.Telephone != tenant.Telephone)
            {
                tenant.Telephone = req.Telephone;
            }
            if (req.Legalperson != tenant.Legalperson)
            {
                tenant.Legalperson = req.Legalperson;
            }
            if (req.Businesslicense_img != tenant.Businesslicense_img)
            {
                tenant.Businesslicense_img = req.Businesslicense_img;
            }
            if (req.Name != tenant.Name)
            {
                tenant.Name = req.Name;
            }
            if (req.Usci != tenant.Usci)
            {
                tenant.Usci = req.Usci;
            }
            return await _repository.UpdateAsync(tenant);
        }
    }
}